﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strMessageID = Request.QueryString["MessageID"];
            if (strMessageID != "add")
            {
                int MessageID;
                if (!int.TryParse(strMessageID, out MessageID))
                    Response.Redirect("~/");
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                DataAccessLayer.Message Message = DataAccessLayer.Message.FindByMessageID_IncludeDeleted(db, MessageID);
                if (Message == null)
                    Response.Redirect("~/");
                FromDatabase(Message);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Message.Convert(DataAccessLayer.Message.HistoryToListByMessageID(db, MessageID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strMessageID = Request.QueryString["MessageID"];
                string strRequestID = Request.QueryString["RequestID"];
                if (strMessageID != "add")
                {
                    int MessageID = int.Parse(strMessageID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Message Message = DataAccessLayer.Message.FindByMessageID_IncludeDeleted(db, MessageID);
                    DataAccessLayer.Message NewMessage = new DataAccessLayer.Message();
                    NewMessage.MessageID = MessageID;
                    NewMessage.EditorUserID = UserID;
                    NewMessage.Timestamp = DateTime.Now;
                    ToDatabase(NewMessage);
                    db.Messages.InsertOnSubmit(NewMessage);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    int RequestID = int.Parse(strRequestID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sMessage sMessage = new DataAccessLayer.sMessage();
                    sMessage.RequestID = RequestID;
                    db.sMessages.InsertOnSubmit(sMessage);
                    db.SubmitChanges();
                    DataAccessLayer.Message NewMessage = new DataAccessLayer.Message();
                    NewMessage.MessageID = sMessage.ID;
                    NewMessage.EditorUserID = UserID;
                    NewMessage.Timestamp = DateTime.Now;
                    ToDatabase(NewMessage);
                    db.Messages.InsertOnSubmit(NewMessage);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strMessageID = Request.QueryString["MessageID"];
                int MessageID = int.Parse(strMessageID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Message Message = DataAccessLayer.Message.FindByMessageID_IncludeDeleted(db, MessageID);
                DataAccessLayer.Message NewMessage = new DataAccessLayer.Message();
                NewMessage.MessageID = MessageID;
                NewMessage.EditorUserID = UserID;
                NewMessage.Timestamp = DateTime.Now;
                NewMessage.IsDeleted = true;
                ToDatabase(NewMessage);
                db.Messages.InsertOnSubmit(NewMessage);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Message Message)
        {
            txtSender.Text = Message.Sender;
            txtMessageContent.Text = Message.Message1;
            txtDescription.Text = Message.Description;
            ddlAnnounce.SelectedValue = Message.Announce ? "1" : "0";
            lblDeletedItem.Visible = Message.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Message Message)
        {
            Message.Sender = txtSender.Text;
            Message.Message1 = txtMessageContent.Text;
            Message.Description = txtDescription.Text;
            Message.Announce = ddlAnnounce.SelectedValue == "1";
        }
    }
}